/// Copyright 2007-8 Faraday Media
/// 
/// Licensed under the Apache License, Version 2.0 (the "License"); 
/// you may not use this file except in compliance with the License. 
/// You may obtain a copy of the License at 
/// 
///   http://www.apache.org/licenses/LICENSE-2.0 
/// 
/// Unless required by applicable law or agreed to in writing, software 
/// distributed under the License is distributed on an "AS IS" BASIS, 
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
/// See the License for the specific language governing permissions and 
/// limitations under the License.
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace MetaSharp.XML {
  ///<summary>
  /// The DictionaryOfLists provides a container for working with dictionaries that can contain multiple value with the same key.
  ///</summary>
  ///<typeparam name="TKey">the key type</typeparam>
  ///<typeparam name="TValue">the element type</typeparam>
  public class DictionaryOfLists<TKey, TValue> : IDictionary<TKey, IList<TValue>>  {
    private IDictionary<TKey, IList<TValue>> mUnderlying = new Dictionary<TKey, IList<TValue>>();

    ///<summary>
    /// Creates a new empty DictionaryOfLists
    ///</summary>
    public DictionaryOfLists() {
    }

    ///<summary>
    /// Retrieves a read-only view of the dictionary.
    ///</summary>
    public IReadOnlyDictionary<TKey, IList<TValue>> ReadOnlyDictionary {
      get {
        return new ReadOnlyDictionary<TKey, IList<TValue>>(this);
      }
    }

    #region IDictionary<TKey,TValue> Members

    ///<summary>
    ///Determines whether the <see cref="T:System.Collections.Generic.IDictionary`2"></see> contains an element with the specified key.
    ///</summary>
    ///
    ///<returns>
    ///true if the <see cref="T:System.Collections.Generic.IDictionary`2"></see> contains an element with the key; otherwise, false.
    ///</returns>
    ///
    ///<param name="key">The key to locate in the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.</param>
    ///<exception cref="T:System.ArgumentNullException">key is null.</exception>
    public bool ContainsKey(TKey key) {
      return mUnderlying.ContainsKey(key);
    }

    ///<summary>
    ///Adds an element with the provided key and value to the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.
    ///</summary>
    ///
    ///<param name="value">The object to use as the value of the element to add.</param>
    ///<param name="key">The object to use as the key of the element to add.</param>
    ///<exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"></see> is read-only.</exception>
    ///<exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.</exception>
    ///<exception cref="T:System.ArgumentNullException">key is null.</exception>
    public void Add(TKey key, IList<TValue> value) {
      mUnderlying.Add(key, value);
    }

    ///<summary>
    ///</summary>
    ///<param name="key"></param>
    ///<param name="value"></param>
    public void Add(TKey key, TValue value) {
      if (!mUnderlying.ContainsKey(key)) {
        mUnderlying.Add(key, new List<TValue>());
      }
      mUnderlying[key].Add(value);
    }

    ///<summary>
    ///Removes the element with the specified key from the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.
    ///</summary>
    ///
    ///<returns>
    ///true if the element is successfully removed; otherwise, false.  This method also returns false if key was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2"></see>.
    ///</returns>
    ///
    ///<param name="key">The key of the element to remove.</param>
    ///<exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"></see> is read-only.</exception>
    ///<exception cref="T:System.ArgumentNullException">key is null.</exception>
    public bool Remove(TKey key) {
      return mUnderlying.Remove(key);
    }

    ///<summary>
    ///Removes the specified value from the list for the given key.
    ///</summary>
    ///
    ///<returns>
    ///true if the element is successfully removed; otherwise, false.  This method also returns false if key was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2"></see>.
    ///</returns>
    ///
    ///<param name="key">The key of the element to remove.</param>
    /// <param name="value">the value to remove from the key's list</param>
    ///<exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"></see> is read-only.</exception>
    ///<exception cref="T:System.ArgumentNullException">key is null.</exception>
    public bool Remove(TKey key, TValue value) {
      if (mUnderlying.ContainsKey(key)) {
        bool result = mUnderlying[key].Remove(value);

        if (result && mUnderlying[key].Count == 0) {
          mUnderlying.Remove(key);
        }

        return result;
      }

      return false;
    }

    ///<summary>
    ///</summary>
    ///<param name="key"></param>
    ///<param name="value"></param>
    ///<returns></returns>
    public bool TryGetValue(TKey key, out IList<TValue> value) {
      return mUnderlying.TryGetValue(key, out value);
    }

    ///<summary>
    ///Gets or sets the element with the specified key.
    ///</summary>
    ///
    ///<returns>
    ///The element with the specified key.
    ///</returns>
    ///
    ///<param name="key">The key of the element to get or set.</param>
    ///<exception cref="T:System.NotSupportedException">The property is set and the <see cref="T:System.Collections.Generic.IDictionary`2"></see> is read-only.</exception>
    ///<exception cref="T:System.ArgumentNullException">key is null.</exception>
    ///<exception cref="T:System.Collections.Generic.KeyNotFoundException">The property is retrieved and key is not found.</exception>
    public IList<TValue> this[TKey key] {
      get { return mUnderlying[key]; }
      set { mUnderlying[key] = value; }
    }

    ///<summary>
    ///Gets an <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the keys of the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.
    ///</summary>
    ///
    ///<returns>
    ///An <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the keys of the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"></see>.
    ///</returns>
    ///
    public ICollection<TKey> Keys {
      get { return new ReadOnlyCollection<TKey>(new List<TKey>(mUnderlying.Keys)); }
    }

    ///<summary>
    ///Gets an <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the values in the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.
    ///</summary>
    ///
    ///<returns>
    ///An <see cref="T:System.Collections.Generic.ICollection`1"></see> containing the values in the object that implements <see cref="T:System.Collections.Generic.IDictionary`2"></see>.
    ///</returns>
    ///
    public ICollection<IList<TValue>> Values {
      get { return new ReadOnlyCollection<IList<TValue>>(new List<IList<TValue>>(mUnderlying.Values)); }
    }

    #endregion

    #region ICollection<KeyValuePair<TKey,TValue>> Members

    ///<summary>
    ///Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
    ///</summary>
    ///
    ///<param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
    ///<exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception>
    public void Add(KeyValuePair<TKey, IList<TValue>> item) {
      mUnderlying.Add(item);
    }

    ///<summary>
    ///Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
    ///</summary>
    ///
    ///<exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only. </exception>
    public void Clear() {
      mUnderlying.Clear();
    }

    ///<summary>
    ///Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"></see> contains a specific value.
    ///</summary>
    ///
    ///<returns>
    ///true if item is found in the <see cref="T:System.Collections.Generic.ICollection`1"></see>; otherwise, false.
    ///</returns>
    ///
    ///<param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
    public bool Contains(KeyValuePair<TKey, IList<TValue>> item) {
      return mUnderlying.Contains(item);
    }

    ///<summary>
    ///Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"></see> to an <see cref="T:System.Array"></see>, starting at a particular <see cref="T:System.Array"></see> index.
    ///</summary>
    ///
    ///<param name="array">The one-dimensional <see cref="T:System.Array"></see> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"></see>. The <see cref="T:System.Array"></see> must have zero-based indexing.</param>
    ///<param name="arrayIndex">The zero-based index in array at which copying begins.</param>
    ///<exception cref="T:System.ArgumentOutOfRangeException">arrayIndex is less than 0.</exception>
    ///<exception cref="T:System.ArgumentNullException">array is null.</exception>
    ///<exception cref="T:System.ArgumentException">array is multidimensional.-or-arrayIndex is equal to or greater than the length of array.-or-The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"></see> is greater than the available space from arrayIndex to the end of the destination array.-or-Type T cannot be cast automatically to the type of the destination array.</exception>
    public void CopyTo(KeyValuePair<TKey, IList<TValue>>[] array, int arrayIndex) {
      mUnderlying.CopyTo(array, arrayIndex);
    }

    ///<summary>
    ///Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
    ///</summary>
    ///
    ///<returns>
    ///true if item was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"></see>; otherwise, false. This method also returns false if item is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"></see>.
    ///</returns>
    ///
    ///<param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
    ///<exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception>
    public bool Remove(KeyValuePair<TKey, IList<TValue>> item) {
      return mUnderlying.Remove(item);
    }

    ///<summary>
    ///Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
    ///</summary>
    ///
    ///<returns>
    ///The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
    ///</returns>
    ///
    public int Count {
      get { return mUnderlying.Count; }
    }

    ///<summary>
    ///Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.
    ///</summary>
    ///
    ///<returns>
    ///true if the <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only; otherwise, false.
    ///</returns>
    ///
    public bool IsReadOnly {
      get { return true; }
    }

    #endregion

    #region IEnumerable<KeyValuePair<TKey,TValue>> Members

    IEnumerator<KeyValuePair<TKey, IList<TValue>>> IEnumerable<KeyValuePair<TKey, IList<TValue>>>.GetEnumerator() {
      return mUnderlying.GetEnumerator();
    }

    #endregion

    #region IEnumerable Members

    ///<summary>
    ///Returns an enumerator that iterates through a collection.
    ///</summary>
    ///
    ///<returns>
    ///An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
    ///</returns>
    ///<filterpriority>2</filterpriority>
    public IEnumerator GetEnumerator() {
      return mUnderlying.GetEnumerator();
    }

    #endregion
  }
}
